package com.forsythia.sns.struts2.interceptor;

import org.apache.log4j.Logger;

import com.forsythia.sns.action.BaseAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

/**
 * All action should be a subclass of com.forsythia.sns.action.BaseAction.
 * 
 * This interceptor should be follow the org.apache.struts2.interceptor.CookieInterceptor.
 * 
 * @author changwei
 * 
 */
public class LoginInterceptor implements Interceptor {

    private static final long serialVersionUID = 2572400249381407517L;
    private Logger log = Logger.getLogger(this.getClass());

    @Override
    public void destroy() {
        log.info("[LoginInterceptor] destoried.");
    }

    @Override
    public void init() {
        log.info("[LoginInterceptor] initialized.");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        if (invocation.getAction() instanceof BaseAction) {
            BaseAction baseAction = (BaseAction) invocation.getAction();
            if (baseAction.isLogined()) {
                return invocation.invoke();
            }
        }
        log.info("[LoginInterceptor] No User Session Info found.");
        return "need.login";
    }

}
